package com.jm.weibo.ui;

import java.util.Date;
import java.util.List;

import weibo4andriod.Comment;
import weibo4andriod.Status;
import weibo4andriod.WeiboException;
import android.app.Activity;
import android.content.Intent;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.jm.weibo.logic.IWeiboActivity;
import com.jm.weibo.logic.MainService;
import com.jm.weibo.util.AppContainer;
import com.jm.weibo.util.TextViewLink;

 

public class ViewWeiboActivity extends Activity implements OnClickListener,IWeiboActivity{

	private View title;
	
	private Button back, home;
	
	//显示用户的布局
	private RelativeLayout tweet_profile;
	
	// 下面的四个选项
	private TextView tvRefresh, tvComment, tvForward, tvFav, tvMore;
	
	// 用户名字 博客内容
	private TextView tweet_profile_name, tweet_message;
	
	// 用户头像 是否VIP
	private ImageView tweet_profile_preview, tweet_profile_vip;
	
	// 转发内容的布局 默认为gone
	private View src_text_block;
	
	// 转发内容
	private TextView tweet_oriTxt;
	
	// 转发内容的图片 默认为gone
	private ImageView tweet_upload_pic2;
	
	//转发内容下面的图片 具体作用不清楚 默认为gone
	private ImageView tweet_upload_pic;
	
	// 					时间              		 评论				转发				 来自
	private TextView tweet_updated, tweet_comment, tweet_redirect, tweet_via;
	
	private static final int REFRESH = 0;
	
	private static final int COMMENT = 1;//评论
	
	private static final int FORWARD = 2;
	
	private static final int FAV = 3;
	
	private static final int MORE = 4;
	
	private static final int USER = 5;
	
	private static final int COMMENTLIST = 6;//评论列表
	
	private Status status;
	
	private List<Comment> comments = null;
	 
	public void init() {
		// TODO Auto-generated method stub
		
	}

	 
	public void refresh(Object... param) {
		 
		
	}
	protected void onCreate(Bundle savedInstanceState) {
		
		super.onCreate(savedInstanceState);
		//取消标题栏
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		
		//获取传递过来的微博信息
		status=(Status)this.getIntent().getSerializableExtra("status");
		   
		setContentView(R.layout.detailweibo);
		
		//为用户布局添加监听事件
		
		//标签部分的布局<!-- 标签部分 --> 
		tweet_profile = (RelativeLayout) findViewById(R.id.tweet_profile);
		tweet_profile.setTag(USER);
		tweet_profile.setOnClickListener(this);
		
		
		title = findViewById(R.id.detailweibo_title);
		
		back = (Button) title.findViewById(R.id.title_bt_left);
	    //返回
		back.setText(R.string.imageviewer_back);
		
		back.setOnClickListener(new Button.OnClickListener(){

			@Override
			public void onClick(View v) {
				ViewWeiboActivity.this.finish();
			}
			
		});
		//主页
		home = (Button) title.findViewById(R.id.title_bt_right);
		home.setBackgroundResource(R.drawable.title_home);
		
		((TextView) title.findViewById(R.id.textView))
				.setText(R.string.title_mblog_content);
		
		ImageView weiboContentPic = (ImageView) this.findViewById(R.id.weiboContentPic);
		
		if(status.getBmiddle_pic() != null && status.getBmiddle_pic().length() != 0){
			//将weiboContentPic设置为显示的
			weiboContentPic.setVisibility(View.VISIBLE);
			weiboContentPic.setImageResource(R.drawable.loading);
			try {
				weiboContentPic.setImageDrawable(MainService.allImage.get(status.getUser().getId()));
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		Status retweetedStatus = status.getRetweetedStatus();
		//如果转发内容不为空
		if(retweetedStatus !=null){
			//转发内容的布局
			src_text_block = findViewById(R.id.src_text_block);
			src_text_block.setVisibility(View.VISIBLE);
			
			//转发内容
			tweet_oriTxt = (TextView)src_text_block.findViewById(R.id.tweet_oriTxt);
			//设置转发的内容
			tweet_oriTxt.setText(retweetedStatus.getText());
			//转发内容中的图片
			tweet_upload_pic2 = (ImageView)src_text_block.findViewById(R.id.tweet_upload_pic2);
		    
			if(retweetedStatus.getBmiddle_pic() != null && retweetedStatus.getBmiddle_pic().length() != 0){
				
				tweet_upload_pic2.setVisibility(View.VISIBLE);
				
				try {
					if(MainService.allImage.get(retweetedStatus.getUser().getId()) == null){
						//设置缺省图片
						tweet_upload_pic2.setImageResource(R.drawable.loading);
						//启动线程加载图片
						new LoadImageThread(retweetedStatus).start();
					}else{
						//设置图片
						tweet_upload_pic2.setImageDrawable(MainService.allImage.get(retweetedStatus.getUser().getId()));
					}
				    
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		//<!-- 昵称 -->
		tweet_profile_name = (TextView) findViewById(R.id.tweet_profile_name);
		//设置昵称
		tweet_profile_name.setText(status.getUser().getName());
		
		//微博内容
		tweet_message = (TextView) findViewById(R.id.tweet_message);
		//给微博设置内容
		tweet_message.setText(status.getText());
	    //标志微博中的链接
		TextViewLink.addURLSpan(status.getText(), tweet_message);
		 
		//<!-- 头像 -->
		tweet_profile_preview = (ImageView) findViewById(R.id.tweet_profile_preview);
		
		tweet_upload_pic = (ImageView) findViewById(R.id.tweet_upload_pic);
		tweet_updated =(TextView) findViewById(R.id.tweet_updated);
		
		//<!-- 是否是VIP -->
		tweet_profile_vip = (ImageView) findViewById(R.id.tweet_profile_vip);
		if(status.getUser().isVerified()){//如果是VIP
			tweet_profile_vip.setVisibility(View.VISIBLE);
		}
		
		//如果头像已经下载
	    if(MainService.allIcon.get(status.getUser().getId())!=null)
        {	
	    	tweet_profile_preview.setImageDrawable(MainService.allIcon.get(
	    			status.getUser().getId()));
        }else
        {// 设定缺省的图片
        	tweet_profile_preview.setImageResource(R.drawable.portrait);	
        }
		
		Date date = status.getCreatedAt();
		int month = date.getMonth()+1;
		int day = date.getDate();
		int hours = date.getHours();
		int minute = date.getMinutes();
		
		//查看转发按钮
		Button btnTweetRedirect  = (Button) this.findViewById(R.id.btnTweetRedirect);
        Button btnTweetComment = (Button) this.findViewById(R.id.btnTweetComment);
		
		
        try {
        	comments = MainService.mainService.weibo.getComments(String.valueOf(status.getId()));
//        	System.out.println("转发："+MainService.mainService.weibo.getRetweets(status.getId()).size());
		} catch (WeiboException e) {
			 
			e.printStackTrace();
		}
        
        btnTweetComment.setText(new StringBuffer().append("评论").append(comments.isEmpty() ? 0 : comments.size() ));

        //        System.out.println("位置的：" + status.getRateLimitLimit());
        //添加查看评论事件
        btnTweetComment.setTag(COMMENTLIST);
        btnTweetComment.setOnClickListener(this);
        //		
//		tweet_updated.setText(month+"-"+day+"  "+hours+"："+minute);
//		tweet_comment =(TextView) findViewById(R.id.tweet_comment);
//		tweet_comment.setText("评论[未知]");//新浪未对此接口开放
//		tweet_comment.setTag(COMMENTLIST);
//		tweet_comment.setOnClickListener(this);
//		tweet_redirect =(TextView) findViewById(R.id.tweet_redirect);
//		tweet_redirect.setText("转发[未知]");//新浪未对此接口开放
//		tweet_via =(TextView) findViewById(R.id.tweet_via);
//		//显示HTMl
//		tweet_via.setText("来自："+Html.fromHtml(status.getSource()));
		
		
		
		tvRefresh = (TextView) findViewById(R.id.tvReload);
		tvRefresh.setTag(REFRESH);
		tvRefresh.setOnClickListener(this);
		tvComment = (TextView) findViewById(R.id.tvComment);
		tvComment.setTag(COMMENT);
		tvComment.setOnClickListener(this);
		tvForward = (TextView) findViewById(R.id.tvForward);
		tvForward.setTag(FORWARD);
		tvForward.setOnClickListener(this);
		tvFav = (TextView) findViewById(R.id.tvFav);
		tvFav.setTag(FAV);
		tvFav.setOnClickListener(this);
		tvMore = (TextView) findViewById(R.id.tvMore);
		tvMore.setTag(MORE);
		tvMore.setOnClickListener(this);
		init();
	}

	

	public void onClick(View v) {
		int tag = (Integer) v.getTag();
		Intent it = null;
		switch (tag) {
		case REFRESH:
			Toast.makeText(this, "REFRESH", 3000).show();
			break;
		case COMMENT:
			it = new Intent(this,CommentActivity.class);
			it.putExtra("id", status.getId());
			startActivity(it);
			break;
		case FORWARD:
			Toast.makeText(this, "FORWARD", 3000).show();
			break;
		case FAV:
			Toast.makeText(this, "FAV", 3000).show();
			break;
		case MORE:
			Toast.makeText(this, "MORE", 3000).show();
			break;
		case USER:
//			it = new Intent(this,UserActivity.class);
//			it.putExtra("user", status.getUser());
//			startActivity(it);
			break;
		case COMMENTLIST:
//			Toast.makeText(this, "COMMENTLIST", 3000).show();
			Intent intent = new Intent();
			intent.setClass(this, ListCommentsActivity.class);
			//将评论列表放入容器AppContainer.commentsList中
			AppContainer.commentsList = this.comments;
			startActivity(intent);
			break;
		}
	}
	
  private Handler myHandler =  new Handler(){
	  public void handleMessage(android.os.Message msg) {
		  //更新
		  tweet_upload_pic2.setImageDrawable((Drawable) msg.obj);
	  }
  };
   
  
  
	//一个现成来加载微博中所涉及的图片
   private class LoadImageThread extends Thread {
	 
	 private Status status;
	 
	 private BitmapDrawable bitmapDrawable;
	 
	 public LoadImageThread(Status status){
		this.status = status;
	 }  
	 
	 Message message = myHandler.obtainMessage();
	 
	 public void run() {
		while(true){
			bitmapDrawable = MainService.allImage.get(status.getUser().getId());
		    if(bitmapDrawable != null){
		    	break;
		    }
		} 
		message.obj = bitmapDrawable;
		//将message压入Message
		myHandler.sendMessage(message);
	 }
  };
	 
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
	}

	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
	}
}
